﻿using System;
using System.Reflection;
using System.Windows.Forms;
using System.Text;
using System.IO;
using System.Collections.Generic;

namespace BinDataTool
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void ChoseFile_Click(object sender, EventArgs e)
        {
            string exname = ".xls";
            string exsname = ".xlsx";

            this.OpenFile.RestoreDirectory = true;
            //this.openFileDialog1.Filter = "*.xlsx";

            string FileName = "";
            if (this.OpenFile.ShowDialog() == DialogResult.OK)
            {
                FileName = this.OpenFile.FileName;

                FileInfo finfo = new FileInfo(FileName);
                if (finfo.Extension == exname || finfo.Extension == exsname)
                {
                    if (!MeFile.InsertSingle(FileName))
                    {
                        return;
                    }

                    this.SingleFiles.Text += Path.GetFileName(FileName) + ServerParser.strEnd;
                    this.SingleFiles.SelectionLength = this.OutPut.Text.Length;
                    this.SingleFiles.ScrollToCaret();
                }
            }
        }

        private void BinServer()
        {
            try
            {
                if (MeFile.IsEmpty())
                {
                    this.SingleFiles.Text += "全部表格" + ServerParser.strEnd;
                    MeFile.InitFileList();
                }
                string[] allFileName = MeFile.GetNameList();

                Dictionary<string, string> parseFuncs = new Dictionary<string, string>();

                this.OutPut.Text = /*ServerParser.NowTime() + */"开始解析Excel表" + ServerParser.strEnd;
                this.OutPut.SelectionLength = this.OutPut.Text.Length;
                this.OutPut.ScrollToCaret();

                foreach (string fileName in allFileName)
                {
                    ServerParser.ParseServer(fileName);
                    //ClientParser.ParseClient(fileName);
                    this.OutPut.Text += /*ServerParser.NowTime() + */fileName + ".xls\t\t解析完成" + ServerParser.strEnd;
                    this.OutPut.SelectionLength = this.OutPut.Text.Length;
                    this.OutPut.ScrollToCaret();

                    this.SingleFiles.Text += ServerParser.strEnd;

                    Application.DoEvents();
                }

                // 完成
                this.OutPut.Text += /*ServerParser.NowTime() + */"表格全部解析完成" + ServerParser.strEnd;
                this.OutPut.SelectionLength = this.OutPut.Text.Length;
                this.OutPut.ScrollToCaret();

                MeFile.ClearDic();
            }
            catch (System.Exception E)
            {
                this.OutPut.Text += E.Message;
                this.OutPut.SelectionLength = this.OutPut.Text.Length;
                this.OutPut.ScrollToCaret();
                ServerParser.EndParse();
                //ClientParser.EndParse();
            }
            finally
            {
                ServerParser.EndParse();
                //ClientParser.EndParse();
            }
        }

        private void BinJsonData()
        {
            try
            {
                if (MeFile.IsEmpty())
                {
                    this.SingleFiles.Text += "全部表格" + ServerParser.strEnd;
                    MeFile.InitFileList();
                }
                string[] allFileName = MeFile.GetNameList();

                Dictionary<string, string> parseFuncs = new Dictionary<string, string>();

                this.OutPut.Text = /*ServerParser.NowTime() + */"开始解析Excel表" + ServerParser.strEnd;
                this.OutPut.SelectionLength = this.OutPut.Text.Length;
                this.OutPut.ScrollToCaret();

                foreach (string fileName in allFileName)
                {
                    if (BinJson.ParseExcel(fileName, this.OutPut))
                    {
                        //ClientParser.ParseClient(fileName);
                        this.OutPut.Text += /*ServerParser.NowTime() + */fileName + ".xls\t\t解析完成" + ServerParser.strEnd;
                        this.OutPut.SelectionLength = this.OutPut.Text.Length;
                        this.OutPut.ScrollToCaret();
                        this.SingleFiles.Text += ServerParser.strEnd;

                        Application.DoEvents();
                    }
                    else
                    {
                        this.OutPut.Text += /*ServerParser.NowTime() + */fileName + ".xls\t\t解析失败" + ServerParser.strEnd;
                        this.OutPut.SelectionLength = this.OutPut.Text.Length;
                        this.OutPut.ScrollToCaret();
                        this.SingleFiles.Text += ServerParser.strEnd;
                        return;
                    }
                }

                // 完成
                this.OutPut.Text += /*ServerParser.NowTime() + */"表格全部解析完成" + ServerParser.strEnd;
                this.OutPut.SelectionLength = this.OutPut.Text.Length;
                this.OutPut.ScrollToCaret();

                MeFile.ClearDic();
            }
            catch (System.Exception E)
            {
                this.OutPut.Text += E.Message;
                this.OutPut.SelectionLength = this.OutPut.Text.Length;
                this.OutPut.ScrollToCaret();
                ServerParser.EndParse();
                //ClientParser.EndParse();
            }
            finally
            {
                ServerParser.EndParse();
                //ClientParser.EndParse();
            }
        }

        private void BinData_Click(object sender, EventArgs e)
        {
            BinServer();
        }

        private void Json_Click(object sender, EventArgs e)
        {
            BinJsonData();
        }
    }
}
